const db = require('../../lib/conn_oracle')

exports.getMcsDeviceMiddleInfo = async (req, res) => {
    // console.log(req.body)
    let { form } = req.body
    let { lines, stations, specialties, subsys } = form
    let resDatas
    let msg
    let resStatus
    let searchDatasArr = []
    let lineWhere
    let staWhere
    let specWhere
    let subWhere

    if (lines && lines.length === 1) {
        lineWhere = `and n.line = :lines`
        searchDatasArr.push(...lines)
    } else if (lines && lines.length > 1) {
        lineWhere = `and n.line in (:lines`
        for (let i = 0; i < lines.length; i++)
            lineWhere += (i > 0) ? ", :" + i : ":" + i
        lineWhere += ")"
        searchDatasArr.push(...lines)
    } else {
        lineWhere = ""
    }

    if (stations && stations.length === 1) {
        staWhere = `and n.loc = :stations`
        searchDatasArr.push(...stations)
    } else if (stations && stations.length > 1) {
        staWhere = `and n.loc in (:stations`
        for (let i = 0; i < stations.length; i++)
            staWhere += (i > 0) ? ", :" + i : ":" + i
        staWhere += ")"
        searchDatasArr.push(...stations)
    } else {
        staWhere = ""
    }

    if (specialties && specialties.length === 1) {
        specWhere = `and n.spec = :specialties`
        searchDatasArr.push(...specialties)
    } else if (specialties && specialties.length > 1) {
        specWhere = `and n.spec in (:specialties`
        for (let i = 0; i < specialties.length; i++)
            specWhere += (i > 0) ? ", :" + i : ":" + i
        specWhere += ")"
        searchDatasArr.push(...specialties)
    } else {
        specWhere = ""
    }

    if (subsys && subsys.length === 1) {
        subWhere = `and n.sub = :subsys`
        searchDatasArr.push(...subsys)
    } else if (subsys && subsys.length > 1) {
        subWhere = `and n.sub in (:subsys`
        for (let i = 0; i < subsys.length; i++)
            subWhere += (i > 0) ? ", :" + i : ":" + i
        subWhere += ")"
        searchDatasArr.push(...subsys)
    } else {
        subWhere = ""
    }

    let searchSql = `
                     with newdc as (
                        select d.*
                            ,substr(d.deviceid,0,3) as line
                            ,case when substr(d.deviceid,0,6) = 'L3BB02'
                                        then 'L3BZ30' 
                                        else substr(d.deviceid,0,6)
                                        end as loc 
                            ,substr(d.deviceid,7,3) as spec 
                            ,substr(d.deviceid,7,6) as sub 
                    --	from c##wxw.devicecode@xfjc02dblink.gzmetro.com d 
                        from c##wxw.devicecode d
                        )
                    --------------------------------------------------------------------------------------
                    select *
                    from newdc n
                    where 1 = 1
                      and n.sub in ('FASFGW', 'FASFSB', 'FASFYG', 'FASMOS', 'GQTSFD', 'FASQSB', 'FASQXF', 'FASXFS', 'FASFHH')
                      ${lineWhere}
                      ${staWhere}
                      ${specWhere}
                      ${subWhere}                      
                    order by n.deviceid
    `

    try {
        let mcsDeviceMiddleInfo = await db.search(searchSql, searchDatasArr)
        resDatas = { mcsDeviceMiddleInfo }
        msg = mcsDeviceMiddleInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = mcsDeviceMiddleInfo.errorNum ? 0 : 1
        // console.log(mcsDeviceMiddleInfo)
        res.json({
            status: resStatus,
            msg,
            resDatas
        })
    } catch (err) {
        res.json({
            status: 0,
            msg: "查询失败",
            resData: { err }
        })
    }

}

exports.editMcsDeviceMiddle = async (req, res) => {
    // console.log(req.body)
    let { type, form } = req.body
    let { DEVICEID, CODEE, CODEC, STATION } = form
    let resDatas
    let msg
    let msgType
    let resStatus
    let editSql
    let editDatas = []

    if(type === "add"  || type === 'copy'){
        msgType = "新增"
        editSql = `insert into c##wxw.devicecode (deviceid, codee, codec, station) values (:DEVICEID, :CODEE, :CODEC, :STATION)`
        editDatas.push(DEVICEID, CODEE, CODEC, STATION)
    }

    if(type === "update"){
        msgType = "更新"
        editSql = `update c##wxw.devicecode set codee = :CODEE, codec = :CODEC, station = :STATION where deviceid = :DEVICEID`
        editDatas.push(CODEE, CODEC, STATION, DEVICEID)
    }

    if (type === "delete") {
        msgType = "删除"
        editSql = `delete from c##wxw.devicecode where deviceid = :DEVICEID`
        editDatas.push(DEVICEID)
    }

    // console.log(editSql)

    try {
        let newMcsDeviceMiddle = await db.writeTab(editSql, editDatas)
        resDatas = { newMcsDeviceMiddle }
        msg = newMcsDeviceMiddle.errorNum ? msgType + "失败" + newMcsDeviceMiddle : msgType + "成功"
        resStatus = newMcsDeviceMiddle.errorNum ? 0 : 1
        res.json({
            status: resStatus,
            msg,
            resDatas
        })
    } catch (err) {
        res.json({
            status: 0,
            msg: msgType + "编辑失败" + err,
            // resDatas: {err}
        })
    }

}

exports.editManyMcsDeviceMiddle = async (req,res) => {
    let { type, form } = req.body
    // console.log(type)
    // console.log(form)
    let resDatas
    let msg
    let msgType
    let resStatus
    let editSql
    let options = {
        autoCommit:true,
    } 

    if(type === "batchAdd"){
        msgType = "批量新增"
        editSql = `insert into c##wxw.devicecode (deviceid, codee, codec, station) values (:DEVICEID, :CODEE, :CODEC, :STATION)`
    }

    if(type === "batchUpdate"){
        msgType = "批量更新"
        editSql = `update c##wxw.devicecode set codee = :CODEE, codec = :CODEC, station = :STATION where deviceid = :DEVICEID`
    }

    if (type === "batchDel") {
        msgType = "批量删除"
        editSql = `delete from c##wxw.devicecode where deviceid = :DEVICEID`
    }

    // console.log(editSql)

    try {
        let newMcsDeviceMiddle = await db.writeManyTab(editSql,form,options)
        resDatas = { newMcsDeviceMiddle }
        msg = newMcsDeviceMiddle.errorNum ? msgType + "失败" + newMcsDeviceMiddle : msgType + "成功"
        resStatus = newMcsDeviceMiddle.errorNum ? 0 : 1
        res.json({
            status: resStatus,
            msg,
            resDatas
        })
    } catch (err) {
        res.json({
            status: 0,
            msg: msgType + "失败" + err,
            // resDatas: {err}
        })
    }
}